<template>
  <div>
    <div class="top-box">
      <el-button type="success" @click="add">增加机构成员</el-button>
    </div>
    <loading v-if="loading"></loading>
    <div class="pd" v-else>
      <div class="search-box">
        <el-input v-model="searchParam.id" placeholder="请输入编号" class="user-input"></el-input>
			<el-input v-model="searchParam.source_id" placeholder="请输入机构编号" class="user-input"></el-input>
			<el-input v-model="searchParam.member_id" placeholder="请输入成员编号" class="user-input"></el-input>
        <el-button type="primary" class="el-icon-search" @click="search">搜索</el-button>
        <el-button class="el-icon-q" @click="cancel">撤销</el-button>
      </div>

      <el-table :data="listData.list" border fit highlight-current-row style="width:100%">
        <el-table-column label="编号" prop="id"></el-table-column>
        <el-table-column label="机构编号" prop="source_id"></el-table-column>
        <el-table-column label="成员编号" prop="member_id"></el-table-column>
        <el-table-column label="一级分类" prop="first_level_cid"></el-table-column>
        <el-table-column label="二级分类" prop="second_level_cid"></el-table-column>
            <el-table-column label="操作">
              <template scope="scope">
                <i class="i-icon icon-revamp" title="修改"  @click="edit(scope.row)"></i>
                <i class="i-icon icon-delete" title="删除"  @click="remove(scope.row.id)"></i>
              </template>
            </el-table-column>
          </el-table>
      <div class="pagination-container">
        <el-pagination
          @current-change="handleChange"
          :current-page.sync="pageall"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="listData.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="增加机构成员" :visible.sync="showAddModelFlag" size="tiny">
      <div class="input-group">
        <label><i>*</i>机构编号：</label>
        <div class="input-group-box"><el-input v-model="addParam.source_id" placeholder="请输入机构编号"></el-input></div>
      </div>
	  <div class="input-group">
        <label><i>*</i>成员编号：</label>
        <div class="input-group-box"><el-input v-model="addParam.member_id" placeholder="请输入成员编号"></el-input></div>
      </div>
	  <div class="input-group">
        <label><i>*</i>一级分类：</label>
        <div class="input-group-box"><el-input v-model="addParam.first_level_cid" placeholder="请输入一级分类"></el-input></div>
      </div><div class="input-group">
        <label><i>*</i>二级分类：</label>
        <div class="input-group-box"><el-input v-model="addParam.second_level_cid" placeholder="请输入二级分类"></el-input></div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改机构成员" :visible.sync="showUpdateModelFlag" size="tiny">
      <div class="input-group">
        <label><i>*</i>机构编号：</label>
        <div class="input-group-box"><el-input v-model="editParam.source_id" placeholder="请输入机构编号"></el-input></div>
      </div>
	  <div class="input-group">
        <label><i>*</i>成员编号：</label>
        <div class="input-group-box"><el-input v-model="editParam.member_id" placeholder="请输入成员编号"></el-input></div>
      </div>
	  <div class="input-group">
        <label><i>*</i>一级分类：</label>
        <div class="input-group-box"><el-input v-model="editParam.first_level_cid" placeholder="请输入一级分类"></el-input></div>
      </div><div class="input-group">
        <label><i>*</i>二级分类：</label>
        <div class="input-group-box"><el-input v-model="editParam.second_level_cid" placeholder="请输入二级分类"></el-input></div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="showUpdateModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import apiConfig from '../../utils/apiconfig'
import api from '../../utils/api'
import helper from '../../utils/helper'
import loading from '../../components/loading'
export default {
  data () {
    return {
      listData: [],
      pageSize: 20,
      pageall: 1,
      page: 1,
      loading: true,
      showAddModelFlag: false,
      showUpdateModelFlag: false,
      searchParam: {
        id: '',
        source_id: '',
        member_id: ''
      },
      addParam: {
        source_id: '',
        member_id: '',
        first_level_cid: '',
        second_level_cid: ''
      },
      editParam: {
        id: '',
        source_id: '',
        member_id: '',
        first_level_cid: '',
        second_level_cid: ''
      }
    }
  },
  created () {
    this.getList()
  },
  components: {
    loading
  },
  methods: {
    getSearchParam () { // 参数
      var param = {
        page: this.page,
        id: this.searchParam.id,
        source_id: this.searchParam.source_id,
        member_id: this.searchParam.member_id
      }
      this.params = param
    },
    getList () {
      this.getSearchParam()
      api.GET(helper.getManageApiUrl(apiConfig.sourceMemberList), this.params, res => {
        this.listData = res
        this.loading = false
      }, res => {
        console.log(res)
      })
    },
    search () {
      this.getList()
    },
    cancel () {
      this.searchParam.source_id = ''
      this.searchParam.member_id = ''
      this.searchParam.first_level_cid = ''
      this.searchParam.second_level_cid = ''
    },
    handleChange (val) {
      this.page = val
      this.getList()
    },
    add () {
      this.showAddModelFlag = true
    },
    confirmAdd () {
      var param = {
        source_id: this.addParam.source_id,
        member_id: this.addParam.member_id,
        first_level_cid: this.addParam.first_level_cid,
        second_level_cid: this.addParam.second_level_cid,
      }
      api.GET(helper.getManageApiUrl(apiConfig.addSourceMember), param, res => {
        this.$notify({
          title: '成功',
          message: '添加成功',
          type: 'success',
          duration: 1500
        })
        this.addParam.source_id = ''
        this.addParam.member_id = ''
        this.addParam.first_level_cid = ''
        this.addParam.second_level_cid = ''
        this.showAddModelFlag = false
        this.getList()
      }, res => {
        this.$notify({
          title: '失败',
          message: res.msg,
          type: 'fail',
          duration: 1500
        })
      })
    },
    remove (id) {
      var param = {
        id: id
      }
      this.$confirm('确认要删除此条信息吗？')
      .then(_ => {
        api.GET(helper.getManageApiUrl(apiConfig.deleteSourceMember), param, res => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 1500
          })
          this.getList()
        }, res => {
          this.$notify({
            title: '失败',
            message: '删除失败',
            type: 'error',
            duration: 1500
          })
        })
      })
      .catch(_ => {})
    },
    edit (item) {
      this.editParam.id = item.id
      this.editParam.source_id = item.source_id
      this.editParam.member_id = item.member_id
      this.editParam.first_level_cid = item.first_level_cid
      this.editParam.second_level_cid = item.second_level_cid
      this.showUpdateModelFlag = true
    },
    save () {
      api.GET(helper.getManageApiUrl(apiConfig.updateSourceMember), this.editParam, res => {
        this.$notify({
          title: '成功',
          message: '修改成功',
          type: 'success',
          duration: 1500
        })
        this.showUpdateModelFlag = false
        this.getList()
      }, res => {
        this.$notify({
          title: '失败',
          message: '修改失败',
          type: 'error',
          duration: 1500
        })
      })
    }
  }
}
</script>
<style>
 .search-box {
  font-size: 0;
  padding-bottom:10px;
 }
 .search-box .user-input{
   width: 197px;
   margin: 3px 2px;
 }
</style>

